package com.yzc.mihuyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yzc.mihuyou.entity.*;
import com.yzc.mihuyou.mapper.LefImgMapper;
import com.yzc.mihuyou.mapper.LefWueMapper;
import com.yzc.mihuyou.service.LefImgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzc.mihuyou.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 提瓦特地图 服务实现类
 * </p>
 *
 * @author yzc
 * @since 2022-05-15
 */
@Service
public class LefImgServiceImpl extends ServiceImpl<LefImgMapper, Lefimg> implements LefImgService {

    @Autowired
    LefImgMapper lefImgMapper;
    @Autowired
    LefWueMapper lefWueMapper;

    @Override
    public Map<String, Object> importProfession(MultipartFile file) {
        Map<String, Object> map = new HashMap<>();
// 获取文件的名称
        String fileName = file.getOriginalFilename();
        System.out.println(fileName);
// 获取文件的后缀名
        String pattern = fileName.substring(fileName.lastIndexOf(".") + 1);

        System.out.println(pattern);
        List<List<String>> listContent = new ArrayList<>();
        String message = "导入成功";
        try {
            if (file != null) {
                //文件类型判断
                if (!ExcelUtil.isEXCEL(file)) {
                    message = "该文件不是excel文件";
                } else {
                    listContent = ExcelUtil.readExcelContents(file, pattern, 2);
                    //文件内容判断
                    if (listContent.isEmpty()) {
                        message = "表格内容为空";
                    } else {
                        Lefimg lefImg=new Lefimg();
                        lefImg.setLeafletConem("书籍/食谱");
                        lefImgMapper.insert(lefImg);
                        for (int i=0;i<listContent.size();i++){
                            Lefwue lefWue=new Lefwue();
                            lefWue.setLefImg(listContent.get(i).get(1));
                            lefWue.setLefCone(listContent.get(i).get(0));
                            lefWue.setLeafletId(lefImg.getLeafletId());
                            lefWueMapper.insert(lefWue);
                        }
                    }
                }
            } else {
                message = "未选择文件";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("code", 200);
        map.put("msg", message);
        map.put("data", fileName);
        return map;
    }

    @Override
    public Map<String, Object> lewu() {
        List<Lefimg> list=lefImgMapper.selectList(null);
        System.out.println(list);
        Map<String,Object> map=new HashMap<>();
        List list1=new ArrayList();
        for (Lefimg lefImg:list){
            QueryWrapper<Lefwue> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("leaflet_id",lefImg.getLeafletId());
            Map<String,Object> map1=new HashMap<>();
            map1.put("lefImg",lefImg);
            map1.put("LefWue",lefWueMapper.selectList(queryWrapper));
            list1.add(map1);
        }
        map.put("LefImg",list);
        map.put("let",list1);
        return map;
    }
}
